<template>
	<view class="exhibition-container">
		<!-- 加载中 -->
		<view class="loading" v-if="loading">
			<uni-load-more status="loading" />
		</view>
		
		<block v-else>

			
			<!-- 展览图片轮播 -->
			<swiper class="exhibition-gallery" indicator-dots indicator-active-color="#1aad19" autoplay circular :interval="3000" :duration="500">
				<swiper-item v-if="exhibitionImages.length > 0" v-for="(item, index) in exhibitionImages" :key="index">
					<image class="gallery-image" :src="getImageUrl(item.imageUrl)" mode="aspectFill"></image>
				</swiper-item>
				<swiper-item v-else>
					<image class="gallery-image" :src="getImageUrl(exhibition.cover)" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
			
			<!-- 展览标题 -->
			<view class="title-section">
				<view class="title">{{exhibition.title}}</view>
				<view class="exhibition-status" :class="{'status-active': exhibition.status === 1}">
					{{ exhibition.status === 1 ? '展览中' : '未展览' }}
				</view>
			</view>
			
			<!-- 日期和预约人数统计 -->
			<view class="stats-bar">
				<view class="stats-item">
					<uni-icons type="calendar" size="20" color="#1aad19"></uni-icons>
					<text>{{formatDate(exhibition.startTime)}} 至 {{formatDate(exhibition.endTime)}}</text>
				</view>

			</view>
			
			<!-- 展览基本信息 -->
			<view class="info-card card">
				<view class="card-header">
					<view class="section-title">基本信息</view>
					<view class="section-line"></view>
				</view>
				
				<view class="info-list">
					<view class="info-item">
						<view class="info-icon">
							<uni-icons type="location" size="18" color="#1aad19"></uni-icons>
						</view>
						<view class="info-content">
							<text class="label">展览地点</text>
							<text class="value">{{exhibition.location || '博物馆主馆'}}</text>
						</view>
					</view>
					
					<view class="info-item">
						<view class="info-icon">
							<uni-icons type="tag" size="18" color="#1aad19"></uni-icons>
						</view>
						<view class="info-content">
							<text class="label">展览类型</text>
							<text class="value">{{exhibition.category || '常规展览'}}</text>
						</view>
					</view>
					
					<view class="info-item">
						<view class="info-icon">
							<uni-icons type="staff" size="18" color="#1aad19"></uni-icons>
						</view>
						<view class="info-content">
							<text class="label">每日接待</text>
							<text class="value">{{exhibition.dailyCapacity || '不限'}}人</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 展览描述 -->
			<view class="description-card card">
				<view class="card-header">
					<view class="section-title">展览介绍</view>
					<view class="section-line"></view>
				</view>
				<view class="description">{{exhibition.description || '暂无展览介绍'}}</view>
			</view>
			
			<!-- 预约按钮 -->
			<view class="reserve-button-wrap">

				<button class="reserve-button" @click="handleReserve">立即预约</button>
			</view>
		</block>
	</view>
</template>

<script>
	import { getExhibitionDetail, getExhibitionImages } from '@/api/exhibition.js';
	import { getImageUrl } from '@/api/file.js';
	
	export default {
		data() {
			return {
				id: null,
				exhibition: {},
				exhibitionImages: [],
				loading: true
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id;
				this.loadExhibitionDetail();
				// this.loadExhibitionImages();
			}
		},
		methods: {
			// Add getImageUrl to methods
			getImageUrl,
			
			// 加载展览详情
			async loadExhibitionDetail() {
				this.loading = true;
				try {
					const res = await getExhibitionDetail(this.id);
					if (res.data) {
						this.exhibition = res.data;
					} else {
						uni.showToast({
							title: '获取展览详情失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('获取展览详情失败', error);
					uni.showToast({
						title: '获取展览详情失败',
						icon: 'none'
					});
				} finally {
					this.loading = false;
				}
			},
			
			// 加载展览图片
			async loadExhibitionImages() {
				try {
					const res = await getExhibitionImages(this.id);
					if (res.data && res.data.length > 0) {
						this.exhibitionImages = res.data;
					}
				} catch (error) {
					console.error('获取展览图片失败', error);
					// 失败时仍使用封面图，不提示错误
				}
			},
			
			// 格式化日期
			formatDate(dateStr) {
				if (!dateStr) return '';
				const date = new Date(dateStr);
				return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
			},
			
			// 判断展览是否正在进行中
			isActive(exhibition) {
				return exhibition.status === 1;
			},


			// 前往预约页面
			handleReserve() {
				// 检查用户是否登录
				const userInfo = uni.getStorageSync('userInfo');
				if (!userInfo) {
					uni.navigateTo({
						url: '/pages/login/index'
					});
					return;
				}
				
				// 跳转到预约页面
				uni.navigateTo({
					url: `/pages/reservation/index?exhibitionId=${this.id}`
				});
			}
		}
	}
</script>

<style lang="scss">
	.exhibition-container {
		background-color: #f8f8f8;
		min-height: 100vh;
		padding-bottom: 120rpx;
		
		.loading {
			padding: 60rpx 0;
			display: flex;
			justify-content: center;
		}
		
		.nav-back {
			position: fixed;
			top: 40rpx;
			left: 30rpx;
			z-index: 100;
			width: 70rpx;
			height: 70rpx;
			border-radius: 50%;
			background-color: rgba(0, 0, 0, 0.3);
			display: flex;
			align-items: center;
			justify-content: center;
		}
		
		.exhibition-gallery {
			width: 100%;
			height: 500rpx;
			
			.gallery-image {
				width: 100%;
				height: 100%;
				display: block;
			}
		}
		
		.title-section {
			background: #fff;
			padding: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: flex-start;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.03);
			
			.title {
				flex: 1;
				font-size: 38rpx;
				font-weight: bold;
				color: #333;
				line-height: 1.4;
			}
			
			.exhibition-status {
				min-width: 120rpx;
				text-align: center;
				font-size: 24rpx;
				padding: 6rpx 16rpx;
				border-radius: 6rpx;
				background-color: #f5f5f5;
				color: #999;
				margin-left: 20rpx;
				margin-top: 8rpx;
				
				&.status-active {
					background-color: rgba(74, 107, 223, 0.1);
					color: #4a6bdf;
				}
			}
		}
		
		.stats-bar {
			display: flex;
			background-color: #fff;
			padding: 0 30rpx 30rpx;
			border-bottom: 1px solid #f5f5f5;
			
			.stats-item {
				display: flex;
				align-items: center;
				font-size: 26rpx;
				color: #666;
				margin-right: 30rpx;
				
				text {
					margin-left: 10rpx;
				}
			}
		}
		
		.card {
			margin: 30rpx;
			border-radius: 16rpx;
			background-color: #fff;
			padding: 30rpx;
			box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
			
			.card-header {
				display: flex;
				align-items: center;
				margin-bottom: 24rpx;
				
				.section-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
					position: relative;
					padding-left: 20rpx;
					
					&::before {
						content: '';
						position: absolute;
						left: 0;
						top: 6rpx;
						bottom: 6rpx;
						width: 6rpx;
						background-color: #4a6bdf;
						border-radius: 3rpx;
					}
				}
				
				.section-line {
					flex: 1;
					height: 1rpx;
					background-color: #f0f0f0;
					margin-left: 20rpx;
				}
			}
		}
		
		.info-card {
			.info-list {
				.info-item {
					display: flex;
					align-items: flex-start;
					margin-bottom: 20rpx;
					
					&:last-child {
						margin-bottom: 0;
					}
					
					.info-icon {
						margin-top: 4rpx;
						margin-right: 16rpx;
					}
					
					.info-content {
						flex: 1;
						
						.label {
							font-size: 26rpx;
							color: #999;
							margin-bottom: 4rpx;
							display: block;
						}
						
						.value {
							font-size: 30rpx;
							color: #333;
						}
					}
				}
			}
		}
		
		.description-card {
			.description {
				font-size: 28rpx;
				color: #666;
				line-height: 1.8;
			}
		}
		
		.reserve-button-wrap {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			padding: 20rpx 30rpx;
			background-color: #fff;
			box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
			display: flex;
			align-items: center;
			
			.share-button {
				width: 160rpx;
				height: 90rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				background-color: #f8f8f8;
				border-radius: 45rpx;
				margin-right: 20rpx;
				
				text {
					font-size: 24rpx;
					color: #666;
					margin-top: 4rpx;
				}
			}
			
			.reserve-button {
				flex: 1;
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				background: linear-gradient(to right, #4a6bdf, #644ae8);
				color: #fff;
				font-size: 32rpx;
				font-weight: bold;
				border-radius: 45rpx;
			}
		}
	}
</style> 